package com.king.student.biz;

import java.util.List;
import java.util.Scanner;

import com.king.student.dao.ClassDao;
import com.king.student.dao.CourseDao;
import com.king.student.dao.StudentCourseDao;
import com.king.student.dao.StudentDao;
import com.king.student.entity.Clazz;
import com.king.student.entity.Course;
import com.king.student.entity.Student;
import com.king.student.entity.StudentCourse;

@SuppressWarnings("resource")
public class AdminController {

	public static void start(){
		menu();
	}
	
	private static void menu(){
		System.out.println("******主菜单********");
		System.out.println("**1.班级管理              ***");
		System.out.println("**2.学生管理              ***");
		System.out.println("**3.课程管理              ***");
		System.out.println("**4.成绩管理              ***");
		System.out.println("**5.退出系统              ***");
		System.out.println("*******************");
		System.out.print("请输入对应功能的编号：");
		switch(new Scanner(System.in).nextInt()){
			
			case 1:
				menu1();
				break;
			case 2:
				menu2();
				break;
			case 3:
				menu3();
				break;
			case 4:
				menu4();
				break;
			case 5:
				System.out.println("Good Bye!");
				System.exit(0);
			default: menu();
		}
		menu();
	}

	private static void menu1(){
		System.out.println("******班级管理*******");
		System.out.println("**1.添加班级               ***");
		System.out.println("**2.删除班级               ***");
		System.out.println("**3.修改班级信息       ***");
		System.out.println("**4.查询班级               ***");
		System.out.println("**5.返回上级               ***");
		System.out.println("*******************");
		System.out.print("请输入对应功能的编号：");
		boolean is;
		Clazz clazz;
		Scanner sc = new Scanner(System.in);
		switch(new Scanner(System.in).nextInt()){
			case 1:
				System.out.print("请依次输入班级编号、班级名，信息间用空格隔开：");
				clazz = new Clazz();
				clazz.setClassID(sc.next());
				clazz.setClassName(sc.next());
				is = ClassDao.addClass(clazz);
				System.out.println("添加"+(is?"成功":"失败"));
				break;
			case 2:
				System.out.print("请输入班级编号：");
				is = ClassDao.delClass(new Scanner(System.in).next());
				System.out.println("删除"+(is?"成功":"失败"));
				break;
			case 3:
				System.out.print("请输入班级编号：");
				String classID = sc.nextLine();
				clazz = ClassDao.getClassByClassID(classID);
				
				if(clazz==null){
					System.out.println("班级不存在");
					break;
				}
				
				System.out.println("原班级信息->  班级编号："+clazz.getClassID()+"   班级名称："+clazz.getClassName());
				System.out.println("请输入新的班级名称：");
				clazz.setClassName(sc.next());
				is = ClassDao.updateClass(clazz);
				System.out.println("修改"+(is?"成功":"失败"));
				break;
			case 4:
				List<Clazz> list = ClassDao.getAllClass();
				for(Clazz temp : list){
					System.out.println(temp);
				}
				break;
			case 5:
				menu();
				break;
				
			default:menu1();
		}
		menu1();
	}

	private static void menu2(){
		System.out.println("******学生管理*******");
		System.out.println("**1.添加学生               ***");
		System.out.println("**2.删除学生               ***");
		System.out.println("**3.修改学生信息       ***");
		System.out.println("**4.查询学生               ***");
		System.out.println("**5.返回上级               ***");
		System.out.println("*******************");
		System.out.print("请输入对应功能的编号：");
		boolean is;
		Student student;
		Scanner sc = new Scanner(System.in);
		switch(new Scanner(System.in).nextInt()){
			case 1:
				System.out.println("请依输入学号、姓名、性别、生日(格式：1980-01-01)、班级编号，信息间用空格隔开：");
				student = new Student();
				student.setStudentID(sc.next());
				student.setStudentName(sc.next());
				student.setStudentSex("男".equals(sc.next())?0:1);
				student.setStudentBirthday(sc.next());
				student.setClassID(sc.next());
				is = StudentDao.addStudent(student);
				System.out.println("添加"+(is?"成功":"失败"));
				break;
			case 2:
				System.out.print("请输入要删除的学生的学号：");
				is = StudentDao.delStudent(sc.next());
				System.out.println("删除"+(is?"成功":"失败"));
				break;
			case 3:
				System.out.print("请输入要修改信息的学生的学号：");
				String studentID = sc.next();
				student = StudentDao.getStudentByStudentID(studentID);
				
				if(student==null){
					System.out.println("学生不存在");
					break;
				}
				
				System.out.println("原学生信息:");
				System.out.println("学号："+student.getStudentID());
				System.out.println("姓名："+student.getStudentName());
				System.out.println("性别："+(student.getStudentSex()==0?"男":"女"));
				System.out.println("生日："+student.getStudentBirthday());
				System.out.println("班级编号："+student.getClassID());
				System.out.println("请依次输入新的学生信息->姓名、性别、生日、班级编号，信息间空格隔开：");
				student.setStudentName(sc.next());
				student.setStudentSex("男".equals(sc.next())?0:1);
				student.setStudentBirthday(sc.next());
				student.setClassID(sc.next());
				is = StudentDao.updateStudent(student);
				System.out.println("修改"+(is?"成功":"失败"));
				break;
			case 4:
				menu21();
				break;
			case 5:
				menu();
				break;
				
			default:menu2();
		}
		menu2();
	}
	
	private static void menu21() {
		System.out.println("******查询学生*******");
		System.out.println("**1.学号查询               ***");
		System.out.println("**2.模糊查询               ***");
		System.out.println("**3.班级查询               ***");
		System.out.println("**4.返回上级               ***");
		System.out.println("*******************");
		System.out.print("请输入对应功能的编号：");
		Student student;
		Scanner sc = new Scanner(System.in);
		switch(new Scanner(System.in).nextInt()){
			case 1:
				System.out.print("请输入要查询的学生学号：");
				student = StudentDao.getStudentByStudentID(sc.next());
				if(student==null){
					System.out.println("学生不存在");
					break;
				}
				System.out.println(student);
				break;
			case 2:
				System.out.print("请输入要查询的学生关键字：");
				List<Student> fuzzyList = StudentDao.getStudentByFuzzyQuery(sc.next());
				for(Student temp : fuzzyList){
					System.out.println(temp);
				}
				break;
			case 3:
				System.out.print("请输入班级编号查询学生：");
				List<Student> classQueryList = StudentDao.getStudentByClassID(sc.next());
				for(Student temp : classQueryList){
					System.out.println(temp);
				}
				break;
			case 4:
				menu2();
				break;
				
			default:menu21();
		}
		menu21();
	}

	private static void menu3(){
		System.out.println("******课程管理*******");
		System.out.println("**1.添加课程               ***");
		System.out.println("**2.删除课程               ***");
		System.out.println("**3.修改课程信息       ***");
		System.out.println("**4.查询课程               ***");
		System.out.println("**5.返回上级               ***");
		System.out.println("*******************");
		System.out.print("请输入对应功能的编号：");
		Scanner sc = new Scanner(System.in);
		Course course;
		boolean is=false;
		switch(new Scanner(System.in).nextInt()){
			case 1:
				System.out.println("请依次输入课程号、课程名、任课老师，信息间用空格隔开：");
				course = new Course();
				course.setCourseID(sc.next());
				course.setCourseName(sc.next());
				course.setCourseTeacher(sc.next());
				is = CourseDao.addCourse(course);
				System.out.println("添加"+(is?"成功":"失败"));
				break;
			case 2:
				System.out.print("请输入要删除的课程的编号：");
				is = CourseDao.delCourse(sc.next());
				System.out.println("删除"+(is?"成功":"失败"));
				break;
			case 3:
				System.out.print("请输入要修改的课程的编号：");
				course = CourseDao.getCourseByCourseID(sc.next());
				
				if(course==null){
					System.out.println("课程不存在");
					break;
				}
				System.out.println("原课程信息："+course);
				System.out.println("请依次输入课程名、任课老师，信息间用空格隔开：");
				course.setCourseName(sc.next());
				course.setCourseTeacher(sc.next());
				is = CourseDao.updateCourse(course);
				System.out.println("修改"+(is?"成功":"失败"));
				break;
			case 4:
				List<Course> list = CourseDao.getCourseAll();
				for(Course temp : list){
					System.out.println(temp);
				}
				break;
			case 5:
				menu();
				break;
				
			default:menu3();
		}
		menu3();
	}
	
	private static void menu4(){
		System.out.println("******成绩管理*******");
		System.out.println("**1.录入成绩               ***");
		System.out.println("**2.删除成绩               ***");
		System.out.println("**3.修改成绩               ***");
		System.out.println("**4.查询成绩               ***");
		System.out.println("**5.返回上级               ***");
		System.out.println("*******************");
		System.out.print("请输入对应功能的编号：");
		
		Scanner sc = new Scanner(System.in);
		Student student;
		boolean is = false;
		switch(new Scanner(System.in).nextInt()){
			case 1:
				{//1.录入成绩
					System.out.print("请输入学生学号：");
					student = StudentDao.getStudentByStudentID(sc.next());
					if(student==null){
						System.out.println("该学生不存在");
						break;
					}
					List<StudentCourse> studentCourseList = StudentCourseDao.getStudentCourseByStudentID(student.getStudentID());
					
					int count = 0;
					for(StudentCourse temp:studentCourseList){
						if(temp.getGrade()==0){
							System.out.println(CourseDao.getCourseByCourseID(temp.getCourseID()));
							count++;
						}
					}
				
					if(count==0){
						System.out.println("无可录入的成绩");
						break;
					}
				
					System.out.print("请输入列表中课程的课程编号：");
					String courseID = sc.next();
					System.out.print("请输入该课程的成绩：");
					int grade = sc.nextInt();
					StudentCourse studentCourse = new StudentCourse();
					studentCourse.setStudentID(student.getStudentID());
					studentCourse.setCourseID(courseID);
					studentCourse.setGrade(grade);
					is = StudentCourseDao.updateStudentCourse(studentCourse);
					System.out.println("录入"+(is?"成功":"失败"));
				}
				
				break;
			case 2:
				{
					//2.删除成绩
					System.out.print("请输入学生学号：");
					student = StudentDao.getStudentByStudentID(sc.next());
					if(student==null){
						System.out.println("该学生不存在");
						break;
					}
					List<StudentCourse> studentCourseList = StudentCourseDao.getStudentCourseByStudentID(student.getStudentID());
					
					int count=0;
					for(StudentCourse temp:studentCourseList){
						if(temp.getGrade()!=0){
							System.out.println(CourseDao.getCourseByCourseID(temp.getCourseID()));
							count++;
						}
					}
					
					if(count==0){
						System.out.println("无可删除的成绩");
						break;
					}
					
					System.out.print("请输入列表中课程的课程编号：");
					String courseID = sc.next();
					is = StudentCourseDao.delStudentCourse(student.getStudentID(), courseID);
					System.out.println("删除"+(is?"成功":"失败"));
				}
				break;
			case 3:
				{
					//3.修改成绩
					System.out.print("请输入学生学号：");
					student = StudentDao.getStudentByStudentID(sc.next());
					if(student==null){
						System.out.println("该学生不存在");
						break;
					}
					List<StudentCourse> studentCourseList = StudentCourseDao.getStudentCourseByStudentID(student.getStudentID());
					
					int count=0;
					for(StudentCourse temp:studentCourseList){
						if(temp.getGrade()!=0){
							System.out.println(CourseDao.getCourseByCourseID(temp.getCourseID()));
							count++;
						}
					}
					
					if(count==0){
						System.out.println("无可修改的成绩");
						break;
					}
					
					System.out.print("请输入列表中课程的课程编号：");
					String courseID = sc.next();
					System.out.print("请输入该课程新的成绩：");
					int grade = sc.nextInt();
					StudentCourse studentCourse = new StudentCourse();
					studentCourse.setStudentID(student.getStudentID());
					studentCourse.setCourseID(courseID);
					studentCourse.setGrade(grade);
					is = StudentCourseDao.updateStudentCourse(studentCourse);
					System.out.println("修改"+(is?"成功":"失败"));
				}
				break;
			case 4:
				{
					//4.查询成绩
					System.out.print("请输入学生学号：");
					student = StudentDao.getStudentByStudentID(sc.next());
					if(student==null){
						System.out.println("该学生不存在");
						break;
					}
					List<StudentCourse> studentCourseList = StudentCourseDao.getStudentCourseByStudentID(student.getStudentID());
					
					for(StudentCourse temp:studentCourseList){
						System.out.println(CourseDao.getCourseByCourseID(temp.getCourseID())+", 成绩："+temp.getGrade());
					}
				}
				break;
			case 5:
				menu();
				break;
				
			default:menu4();
		}
		menu4();
	}
	
	
}
